<?php

class UloginUserIdentity extends CUserIdentity {
    
    const ERROR_ACCOUNT_BANNED = 3;
    const ERROR_ACCOUNT_BLOCKED = 4;
    private $id;
    public $unblockDate;

    public function __construct() {}

    public function authenticate($uloginModel = null) {
        if (empty($uloginModel->identity) || empty($uloginModel->network)) return false;
        $record = Yii::app()->db->createCommand()->select('u.*, g.*')->from('users u')->leftJoin('users_groups g', 'g.groupId=u.groupId')->where('`u`.`u_identity`="'.$uloginModel->identity.'" and `u`.`u_provider`="'.$uloginModel->network.'"')->queryRow();
        if(empty($record)) {
            Yii::app()->db->createCommand()->insert('users', array(
                'email'=>$uloginModel->email, 'login'=>$uloginModel->login, 
                'u_identity'=>$uloginModel->identity, 'u_provider'=>$uloginModel->network, 
                'logDate'=>time(), 'displayName'=>$uloginModel->login, 'regDate'=>time()));
            $record = Yii::app()->db->createCommand()->select('u.*, g.*')->from('users u')->leftJoin('users_groups g', 'g.groupId=u.groupId')->where('`u`.`userid`='.Yii::app()->db->lastInsertID)->queryRow();
            $this->id = $record['userid'];
            $this->setUserStates($record);
            $this->errorCode=self::ERROR_NONE;
        } 
        else if($record['isBanned']) $this->errorCode = self::ERROR_ACCOUNT_BANNED;
        else if($record['unblockDate'] > time()) {
            $this->unblockDate = $record['unblockDate'];
            $this->errorCode = self::ERROR_ACCOUNT_BLOCKED;
        } else {
            $this->id = $record['userid'];
            $this->setUserStates($record);
            Yii::app()->db->createCommand()->update('users', array('logDate'=>time()), 'userid='.$this->getId());
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId() {
        return $this->id;
    }
    
    private function setUserStates($record) {
        $this->setState('login', $record['login']);
        $this->setState('email', $record['email']);
        $this->setState('weight', $record['weight']);
        $this->setState('site', $record['site']);
        $this->setState('twitter', $record['twitter']);
        $this->setState('skype', $record['skype']);
        $this->setState('regDate', $record['regDate']);
        $this->setState('avatar', $record['avatar']);
        $this->setState('groupId', $record['groupId']);
        $this->setState('descr', $record['descr']);
        $this->setState('userid', $record['userid']);
        $this->setState('role', $record['role']);
        $this->setState('displayName', $record['displayName']);
        $this->setState('logDate', time());
        # role options:
        $this->setState('seeAnonim', $record['seeAnonim']);         # видит кто оставил анонимный отзыв
        $this->setState('editReview', $record['editReview']);       # может редакторировать отзывы
        $this->setState('editComment', $record['editComment']);     # может редактирова каменты к отзывам
        $this->setState('moderForum', $record['moderForum']);       # может модерировать минифорум
        $this->setState('moderPartner', $record['moderPartner']);   # может модерировать партнерки и категории
        $this->setState('moderPost', $record['moderPost']);         # может править сообщения к темам на форуме
        $this->setState('moderUser', $record['moderUser']);         # может банить юзеров
        $this->setState('moderNews', $record['moderNews']);         # может править новости
        $this->setState('moderBlog', $record['moderBlog']);         # может править статьи из блога
    }
}